/*
PearReview - The peer review assistant.
Copyright (C) 2009  Dimitrij Pankratz, Anton Musichin
http://www.pearreview.com, info@pearreview.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
package com.pearreview.util.pdf;

import java.io.IOException;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.pdf.BaseFont;

/**
 * iText has no global font class, so this class is meant to deliver a global
 * font to other PDF components. 
 * It is necessary to embed a unicode font
 * to ensure a correct display on different systems. Especially the
 * Cyrillic alphabet has to be supported, because of Russian language support
 * of PearReview.
 *
 */
public final class PdfFontManager {
	
	private static PdfFontManager instance = null;
	private BaseFont baseFont;
	private BaseFont boldBaseFont;
	private final int DEFAULT_SIZE = 12;
	private final String FONT_PATH = getClass().getResource(
		"fonts/LiberationSans-Regular.ttf").toString();
	private final String BOLD_FONT_PATH = getClass().getResource(
		"fonts/LiberationSans-Bold.ttf").toString();
	
	private PdfFontManager() {
		try {
			baseFont = BaseFont.createFont(FONT_PATH, 
					//"Cp1251", //covers languages that use the Cyrillic alphabet
					BaseFont.IDENTITY_H,
					BaseFont.EMBEDDED); //embed the font in PDF file
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static PdfFontManager getInstance() {
		if (instance == null) {
			instance = new PdfFontManager();
		}
		return instance;
	}
	
	public Font getFont() {
		return new Font(baseFont, DEFAULT_SIZE);
	}
	
	public Font getFont(int size) {
		return new Font(baseFont, size);
	}
	
	public BaseFont getBaseFont() {
		return baseFont;
	}
	
	public BaseFont getBoldBaseFont() {
		if (boldBaseFont == null) {
			try {
				boldBaseFont = BaseFont.createFont(BOLD_FONT_PATH,
						BaseFont.IDENTITY_H, //unicode horizontal
						BaseFont.EMBEDDED); //embed the font in PDF file
			} catch (DocumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return boldBaseFont;
	}
}